﻿using System.Collections.Generic;

namespace EternitySolver.Core.Solvers.FixedPath
{
    public class SpiralSolver : FixedPathSolver
    {
        public override LinkedList<int> GetPath(Board board)
        {
            var path = new LinkedList<int>();
            var node = path.AddFirst(0);
            for (var i = 1; i < board.Width; i++)
            {
                node = path.AddAfter(node, i);
            }
            do
            {
                var northsouth = true;
                var eastwest = true;
                for (var k = board.Width - 1; k > 0; k--)
                {
                    for (var j = 0; j < k; j++)
                    {
                        node = northsouth ? path.AddAfter(node, path.Last.Value + board.Width) : path.AddAfter(node, path.Last.Value - board.Width);
                    }
                    for (var j = 0; j < k; j++)
                    {
                        node = eastwest ? path.AddAfter(node, path.Last.Value - 1) : path.AddAfter(node, path.Last.Value + 1);
                    }
                    northsouth = !northsouth;
                    eastwest = !eastwest;
                }

            } while (path.Count < board.PlacedTiles.Count);
            return path;
        }
        public override string ToString()
        {
            return "Spiral Solver";
        }
    }
}